<?php
/**
 * Zend Framework (http://framework.zend.com/)
 *
 * @link      http://github.com/zendframework/Opportunity for the canonical source repository
 * @copyright Copyright (c) 2005-2012 Zend Technologies USA Inc. (http://www.zend.com)
 * @license   http://framework.zend.com/license/new-bsd New BSD License
 */

namespace Opportunity;

use Zend\ModuleManager\Feature\AutoloaderProviderInterface;
use Zend\Mvc\ModuleRouteListener;
use Zend\Mvc\MvcEvent;
use Opportunity\Model\Opportunity;
use Opportunity\Model\Home;
use Opportunity\Model\Detail;
use Opportunity\Model\HomeTable;
use Opportunity\Model\OpportunityTable;
use Opportunity\Model\DetailTable;
use Zend\Db\ResultSet\ResultSet;
use Zend\Db\TableGateway\TableGateway;


class Module implements AutoloaderProviderInterface
{
    public function getAutoloaderConfig()
    {
        return array(
            'Zend\Loader\ClassMapAutoloader' => array(
                __DIR__ . '/autoload_classmap.php',
            ),
            'Zend\Loader\StandardAutoloader' => array(
                'namespaces' => array(
		    // if we're in a namespace deeper than one level we need to fix the \ in the path
                    __NAMESPACE__ => __DIR__ . '/src/' . str_replace('\\', '/' , __NAMESPACE__),
                ),
            ),
        );
    }

    public function getConfig()
    {
        return include __DIR__ . '/config/module.config.php';
    }

    public function onBootstrap(MvcEvent $e)
    {
        // You may not need to do this if you're doing it elsewhere in your
        // application
        $eventManager        = $e->getApplication()->getEventManager();
        $moduleRouteListener = new ModuleRouteListener();
        $moduleRouteListener->attach($eventManager);
    }
    public function getServiceConfig()
    {
    	return array(
    			'factories' => array(
    					'Opportunity\Model\HomeTable' =>  function($sm) {
                     		$tableGateway = $sm->get('HomeTableGateway');
                     		$table = new HomeTable($tableGateway);
                     		return $table;
                 		},
				 		'Opportunity\Model\OpportunityTable' =>  function($sm) {
                     		$tableGateway = $sm->get('OpportunityTableGateway');
                     		$table = new OpportunityTable($tableGateway);
                     		return $table;
                 		},
				 		'Opportunity\Model\DetailTable' =>  function($sm) {
                     		$tableGateway = $sm->get('DetailTableGateway');
                     		$table = new DetailTable($tableGateway);
                     		return $table;
                 		},
                 		'HomeTableGateway' => function ($sm) {
                 			$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                 			$resultSetPrototype = new ResultSet();
                 			$resultSetPrototype->setArrayObjectPrototype(new Home());
                 			return new TableGateway('view_opportunity', $dbAdapter, null, $resultSetPrototype);
                 		},
                 		'OpportunityTableGateway' => function ($sm) {
                 			$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                 			$resultSetPrototype = new ResultSet();
                 			$resultSetPrototype->setArrayObjectPrototype(new Opportunity());
                 			return new TableGateway('opportunity', $dbAdapter, null, $resultSetPrototype);
                 		
                 		},
                 		'DetailTableGateway' => function ($sm) {
                 			$dbAdapter = $sm->get('Zend\Db\Adapter\Adapter');
                 			$resultSetPrototype = new ResultSet();
                 			$resultSetPrototype->setArrayObjectPrototype(new Detail());
                 			return new TableGateway('view_detail_opportunity', $dbAdapter, null, $resultSetPrototype);
                 		},
    			),
    	);
    }
}
